package com.hunan.api.catv.service.impl.tencent_cloud.push;

import com.google.gson.JsonObject;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PushService {

    @Autowired
    RestTemplate restTemplate;

    public JsonObject push(String url, String accessId, String timeStamp, String sign, JsonObject body) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Sign", sign);
        headers.add("AccessId", accessId);
        headers.add("TimeStamp", timeStamp);
        headers.add("Content-Type", "application/json");
        headers.add("charset", "UTF-8");
        HttpEntity<String> request = new HttpEntity<String>(body.toString(), headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
        }
        return null;

    }
}
